"""
ResNet 模型
"""

import torch
import torch.nn as nn

class CBAM(nn.Module):
    def __init__(self, channels, reduction_ratio=16):
        super(CBAM, self).__init__()
        # Channel Attention
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.max_pool = nn.AdaptiveMaxPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channels, channels // reduction_ratio),
            nn.ReLU(),
            nn.Linear(channels // reduction_ratio, channels))
        # Spatial Attention
        self.conv = nn.Conv2d(2, 1, kernel_size=7, padding=3)
        
    def forward(self, x):
        # Channel Attention
        avg_out = self.fc(self.avg_pool(x).squeeze())
        max_out = self.fc(self.max_pool(x).squeeze())
        channel_att = torch.sigmoid(avg_out + max_out).unsqueeze(2).unsqueeze(3)
        x = x * channel_att
        
        # Spatial Attention
        avg_out = torch.mean(x, dim=1, keepdim=True)
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        spatial_att = torch.sigmoid(self.conv(torch.cat([avg_out, max_out], dim=1)))
        return x * spatial_att

"""
# 定义 BasicBlock 模块
# ResNet18/34的残差结构，用的是2个3x3大小的卷积
"""
class BasicBlockCBAM(nn.Module):
    expansion = 1 # 残差结构中，判断主分支的卷积核个数是否发生变化，不变则为1

    def __init__(self, in_channel, out_channel, stride=1, downsample=None, **kwargs): # downsample 对应虚线残差结构
        super(BasicBlockCBAM, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
            kernel_size=3, stride=stride, padding=1, bias=False)

        self.bn1 = nn.BatchNorm2d(out_channel)
        self.relu = nn.ReLU()
        self.cbam1 = CBAM(out_channel);

        self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
            kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channel)
        self.cbam2 = CBAM(out_channel);
        self.downsample = downsample

    def forward(self, x):
        identity = x
        
        # 虚线残差结构，需要下采样
        if self.downsample is not None:
            # 捷径分支short cut
            identity = self.downsample(x)

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.cbam1(out)

        out = self.conv2(out)
        out = self.bn2(out)

        out += identity
        out = self.relu(out)
        out = self.cbam2(out)
        return out

"""
# 定义 Bottleneck 模块
# ResNet50/101/152残差结构， 用的是1x1 + 3x3 + 1x1 的卷积
"""
class BottleneckCBAM(nn.Module):
    """
    # 注意：原论文中，在虚线残差结构的主分支上，第一个1x1卷积层的步距是2，第二个3x3卷积步是1。
    # 但在pytorch官方实现过程中是第一个1x1卷积层的步距是1，第二个3x3卷积层步距是2
    # 这么做的好处是能够在top1上提升大概0.5%的准确率。
    # 可参考Resnet v1.5 https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch
    """
    # 残差结构中的第三层卷积核个数是1/2层卷积核个数的4倍
    expansion = 4
    def __init__(self, in_channel, out_channel, stride=1, downsample=None, groups=1, width_per_group=64):
        super(BottleneckCBAM, self).__init__()

        width = int(out_channel * (width_per_group / 64.)) * groups
        self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=width, kernel_size=1, stride=1, bias=False)
        self.bn1 = nn.BatchNorm2d(width)
        self.cbam1 = CBAM(width)

        self.conv2 = nn.Conv2d(in_channels=width, out_channels=width, groups=groups, kernel_size=3, stride=stride, bias=False, padding=1)
        self.bn2 = nn.BatchNorm2d(width)
        self.cbam2 = CBAM(width)

        self.conv3 = nn.Conv2d(in_channels=width, out_channels=out_channel * self.expansion, kernel_size=1, stride=1, bias=False)
        self.bn3 = nn.BatchNorm2d(out_channel * self.expansion)
        self.cbam3 = CBAM(out_channel * self.expansion)

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample

    def forward(self, x):
        identity = x
        if self.downsample is not None:
            # 捷径分支 short cut
            identity = self.downsample(x)

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.cbam1(out)
        
        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)
        out = self.cbam2(out)

        out = self.conv3(out)
        out = self.bn3(out)

        out += identity
        out = self.relu(out)
        out = self.cbam3(out)
        return out

"""
# 残差网络结构
"""
class ResNetCBAM(nn.Module):
    # block = BasicBlock or Bottleneck
    # blocks_num 为残差结构中 conv2_x ~ conv5_x 中残差块个数，一个列表
    def __init__(self, block, blocks_num, channel=3, groups=1, width_per_group=64):
        super(ResNetCBAM, self).__init__()
        self.channel = channel
        self.in_channel = 64
        self.groups = groups
        self.width_per_group = width_per_group

        self.conv1 = nn.Conv2d(self.channel, self.in_channel, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(self.in_channel)
        self.cbam1 = CBAM(self.in_channel)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, blocks_num[0])
        self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=(2, 1))
        self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=(2, 1))
        self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=(2, 1))

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')

    # channel 为残差结构中第1层卷积核个数
    def _make_layer(self, block, channel, block_num, stride=1):
        downsample = None
        
        # ResNet50/101/152 的残差结构，block.expansion=4
        if stride != 1 or self.in_channel != channel * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(channel * block.expansion),
                CBAM(channel * block.expansion)
            )

        layers = []
        layers.append(block(
            self.in_channel,
            channel,
            downsample=downsample,
            stride=stride,
            groups=self.groups,
            width_per_group=self.width_per_group
        ))
        self.in_channel = channel * block.expansion
        for _ in range(1, block_num):
            layers.append(block(
                self.in_channel,
                channel,
                groups=self.groups,
                width_per_group=self.width_per_group
            ))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.cbam1(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        return x

"""
# resnet34 结构
# https://download.pytorch.org
"""
def resnet34cbam(channel):
    return ResNetCBAM(BasicBlockCBAM, [3, 4, 6, 3], channel)


"""
# resnet50 结构
"""
def resnet50cbam(channel):
    return ResNetCBAM(BottleneckCBAM, [3, 4, 6, 3], channel)

if __name__ == '__main__':
    net = resnet50(1)
    x = torch.randn(2, 1, 32, 120)
    y = net(x)
    print(y.size())